package library.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.view.View;
import android.view.animation.Animation;
import android.webkit.URLUtil;
import android.widget.ImageView;

import java.io.File;
import java.io.InputStream;
import java.lang.ref.WeakReference;

import library.http.HttpTask;

/**
 * Created by Mr.Fan on 2016/2/18.
 */
public class BitmapOption {

    public enum Source {
        resource, assets, file, content, video, url, stream
    }

    private HttpTask httpTask;
    private WeakReference<Context> ref_context;
    private String tag;
    private String flag;
    private Object src;
    private Source from;
    private boolean autoClose = true;
    private float width = 0;
    private float height = 0;
    private float borderWidth = 0;
    private int borderColor = Color.WHITE;
    private float shadowWidth = 0;
    private int shadowColor = Color.BLACK;
    private float corner = 0;
    private boolean grayScale = false;
    private float scaleWidth = 0;
    private float scaleHeight = 0;

    private Animation animation;
    private ImageView.ScaleType scaleType;


    public BitmapOption(Context context) {
        this.ref_context = new WeakReference<>(context);
    }

    public BitmapOption resource(final int resId) {
        this.from = Source.resource;
        this.src = resId;
        return this;
    }

    public BitmapOption httpTask(HttpTask httpTask) {
        this.httpTask = httpTask;
        return this;
    }

    public BitmapOption uri(Uri uri) {
        if (uri != null) {
            String string = uri.toString();
            if (URLUtil.isNetworkUrl(string)) {
                url(string);
            } else if (URLUtil.isAssetUrl(string)) {
                String path = uri.getPath();
                assets(path.substring("/android_asset/".length()));
            } else if (URLUtil.isFileUrl(string)) {
                file(uri.getPath());
            }
        }
        return this;
    }

    public BitmapOption assets(final String path) {
        this.from = Source.assets;
        this.src = path;
        return this;
    }


    public BitmapOption file(final File path) {
        this.from = Source.file;
        this.src = path != null ? path.getAbsolutePath() : null;
        return this;
    }

    public BitmapOption file(final String path) {
        this.from = Source.file;
        this.src = path;
        return this;
    }

    public BitmapOption videoThumbnail(final String path) {
        this.from = Source.video;
        this.src = path;
        return this;
    }

    public BitmapOption inputStream(final InputStream is, boolean autoClose) {
        this.from = Source.stream;
        this.src = is;
        this.autoClose = autoClose;
        return this;
    }

    public BitmapOption url(final String url) {
        this.from = Source.url;
        this.src = url;
        return this;
    }

    public BitmapOption scale(final float width, final float height) {
        this.width = width;
        this.height = height;
        this.scaleWidth = width;
        this.scaleHeight = height;
        return this;
    }


    public BitmapOption size(final float width, final float height) {
        this.width = width;
        this.height = height;
        return this;
    }

    public BitmapOption grayScale(final boolean grayScale) {
        this.grayScale = grayScale;
        return this;
    }

    public BitmapOption border(float borderWidth, int borderColor) {
        this.borderWidth = borderWidth;
        this.borderColor = borderColor;
        return this;
    }

    public BitmapOption border(float borderWidth) {
        this.borderWidth = borderWidth;
        return this;
    }

    public BitmapOption shadow(float shadowWidth, int shadowColor) {
        this.shadowWidth = shadowWidth;
        this.shadowColor = shadowColor;
        return this;
    }

    public BitmapOption shadow(float shadowWidth) {
        this.shadowWidth = shadowWidth;
        return this;
    }

    public BitmapOption round() {
        this.corner = -1;
        return this;
    }

    public BitmapOption round(final float corner) {
        this.corner = corner;
        return this;
    }

    public BitmapOption scaleType(ImageView.ScaleType scaleType) {
        this.scaleType = scaleType;
        return this;
    }


    public BitmapOption animation(Animation animation) {
        this.animation = animation;
        return this;
    }

    public BitmapOption flag(String flag) {
        this.flag = flag;
        return this;
    }

    public BitmapOption tag(final Object obj) {
        this.tag = String.valueOf(obj);
        return this;
    }

    protected boolean isAutoClose() {
        return autoClose;
    }

    protected String getFlag() {
        return flag;
    }

    protected Context getContext() {
        return ref_context != null ? ref_context.get() : null;
    }

    protected String getTag() {
        return tag;
    }

    protected Object getSrc() {
        return src;
    }

    protected Source getFrom() {
        return from;
    }

    protected float getWidth() {
        return width;
    }

    protected float getHeight() {
        return height;
    }

    protected float getCorner() {
        return corner;
    }

    protected boolean isGrayScale() {
        return grayScale;
    }

    protected float getScaleWidth() {
        return scaleWidth;
    }

    protected float getScaleHeight() {
        return scaleHeight;
    }

    protected float getBorderWidth() {
        return borderWidth;
    }

    protected int getBorderColor() {
        return borderColor;
    }

    protected float getShadowWidth() {
        return shadowWidth;
    }

    protected int getShadowColor() {
        return shadowColor;
    }

    protected ImageView.ScaleType getScaleType() {
        return scaleType;
    }

    protected Animation getAnimation() {
        return animation;
    }

    protected HttpTask getHttpTask() {
        return httpTask;
    }


    public void display(final View view) {
        if (view != null) {
            BitmapTask task = new BitmapTask(this, view);
            task.submit();
        }
    }

    public void load(final BitmapCallback callback) {
        if (callback != null) {
            BitmapTask task = new BitmapTask(this, callback);
            task.submit();
        }
    }

    public Bitmap load() {
        BitmapTask task = new BitmapTask(this);
        Bitmap bitmap = task.doInBackground();
        return bitmap;
    }
}
